package com.ucs.ui;

import java.util.List;
import java.util.Map;

import com.ucs.domain.Item;
import com.ucs.domain.Order;
import com.ucs.domain.OrderDetail;
import com.ucs.util.UCSData;

import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;

public class OrderItemListAdapter extends BaseExpandableListAdapter{

	private List<Order>	orders;
	private Map<Integer, Item> id2ItemMap = UCSData.getInstance().getId2ItemMap();
	@Override
	public Object getChild(int groupPosition, int childPosition) {
		return orders.get(groupPosition).getOrderDetails().get(childPosition);
	}

	@Override
	public long getChildId(int groupPosition, int childPosition) {		
		return childPosition;
	}

	@Override
	public View getChildView(int groupPosition, int childPosition,
			boolean isLastChild, View convertView, ViewGroup parentView) {
		if (convertView == null){
			convertView = CategoryItemListAdaptor.getGenericView(parentView);
		}
		OrderDetail orderDetail = (OrderDetail)getChild(groupPosition, childPosition);
		Item item = id2ItemMap.get(orderDetail.getItemID());
		((TextView)convertView).setText(item.getName1());            
		return convertView;        
	}

	@Override
	public int getChildrenCount(int groupPosition) {
		return orders.get(groupPosition).getOrderDetails().size();
	}

	@Override
	public Object getGroup(int groupPosition) {
		return orders.get(groupPosition);
	}

	@Override
	public int getGroupCount() {
		return orders.size();
	}

	@Override
	public long getGroupId(int groupPosition) {
		return groupPosition;
	}

	@Override
	public View getGroupView(int groupPosition, boolean isExpanded,
			View convertView, ViewGroup parentView) {
		TextView textView = CategoryItemListAdaptor.getGenericView(parentView);
		Order order = (Order)getGroup(groupPosition);
		textView.setText(order.getTableID()+", "+order.getTicketNum()+", "+order.getGuestNum()+order.getOrderDetails().size());            
		return textView;        
	}

	@Override
	public boolean hasStableIds() {
		return true;
	}

	@Override
	public boolean isChildSelectable(int groupPosition, int childPosition) {
		return true;
	}
	
//	public static TextView getOrderView(View parentView) {            
//		// Layout parameters for the ExpandableListView            
//		AbsListView.LayoutParams lp = new AbsListView.LayoutParams(40, 64);            
//		TextView textView = new TextView(parentView.getContext());            
//		textView.setLayoutParams(lp);           
//		textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);            
//		// Set the text starting position            
//		textView.setPadding(36, 0, 0, 0);            
//		return textView;        
//	}        

}
